package com.isoft.service.impl;

import com.isoft.entity.DfmeBoreEntity;
import com.isoft.service.DfmeBoreService;
import com.isoft.service.XmlFileHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Path;

@Component
public class DfmeBoreFileHandler implements XmlFileHandler {
    private static final Logger logger = LoggerFactory.getLogger(DfmeBoreFileHandler.class);

    @Autowired
    private DfmeBoreService dfmeBoreService;

    @Override
    public boolean supports(Path filePath) {
        try {
            String fileName = filePath.getFileName().toString().toUpperCase();
            // 支持文件名包含DFME-BORE的XML文件
            return fileName.contains("DFME-BORE") && fileName.endsWith(".XML");
        } catch (Exception e) {
            logger.warn("检查文件支持性时出错: {}", filePath, e);
            return false;
        }
    }

    @Override
    public String handle(Path filePath) throws Exception {
        try {
            logger.info("开始处理DFME-BORE文件: {}", filePath.getFileName());
            
            // 读取文件内容
            String xmlContent = Files.readString(filePath);
            
            // 调用服务处理XML
            String result = dfmeBoreService.parseAndSave(xmlContent);
            
            logger.info("DFME-BORE文件处理完成: {}", filePath.getFileName());
            return result;
        } catch (Exception e) {
            logger.error("处理DFME-BORE文件失败: {}", filePath.getFileName(), e);
            throw e;
        }
    }
} 